package com.cheng.io.buffer;

import java.io.*;

public class BufferedOutputStreamTest01 {
    public static void main(String[] args) {
        m2();
    }

    private static void m1() {
        BufferedOutputStream bufferedOutputStream = null;
        try {
            OutputStream outputStream = new FileOutputStream("io/resources/特性1.txt");
            bufferedOutputStream = new BufferedOutputStream(outputStream);
            bufferedOutputStream.write("你好世界！".getBytes());

            bufferedOutputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 使用try-with-resource改进
     */
    private static void m2() {
        try (
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("io/resources/特性1.txt"))
        ) {
            bufferedOutputStream.write("2025年5月7日".getBytes());
            bufferedOutputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
